//
//  DPViewController.m
//  项目A
//
//  Created by lanou3g on 15/8/28.
//  Copyright (c) 2015年 王钧. All rights reserved.
//

#import "DPViewController.h"
#import "DataParsing.h"
#import "DPCell.h"
#import "URLs.h"
#import "DPModel.h"
#import "UIImage+ImageDowloader.h"
#import "DPSingleton.h"
#import "SDRefresh.h"
#import "DPXQViewController.h"
#import "MJRefresh.h"
#import "AFNetworkReachabilityManager.h"
@interface DPViewController ()
@property (weak, nonatomic) IBOutlet UICollectionView *Collec;
@property (weak, nonatomic) SDRefreshFooterView *refreshFooter;
@property (weak, nonatomic) SDRefreshHeaderView *refreshHeader;
@property (nonatomic, assign) NSInteger totalRowCount;
@property (nonatomic, assign) NSInteger i;
@property (nonatomic, assign) NSInteger x;
@property (nonatomic, assign) NSInteger counts;
@property (nonatomic, strong) DPCell * cell;
@end

@implementation DPViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"单品";
    [self.navigationController.navigationBar setBarTintColor:[UIColor redColor]];
    _i = 0;
    _x = 20;
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status == 2) {
            [[DPSingleton defaultManager] getDPdata:_i];
            [self.Collec reloadData];
            [self checkNetwork];
        }else if (status == 0)
        {
            //        [[DPSingleton defaultManager].DPdata removeAllObjects];
        }else if (status == 1)
        {
            [[DPSingleton defaultManager] getDPdata:_i];
            [self.Collec reloadData];
            [self checkNetwork];
        }
    }];

    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated
{
    
}

// 判断网络
- (void)checkNetwork
{
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status == AFNetworkReachabilityStatusReachableViaWiFi) {
            MJRefreshStateHeader *header = [MJRefreshStateHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
            self.Collec.header = header;
            [self setupFooter];
        }else if (status == AFNetworkReachabilityStatusReachableViaWWAN)
        {
            MJRefreshStateHeader *header = [MJRefreshStateHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
            self.Collec.header = header;
            [self setupFooter];
        }else if (status == AFNetworkReachabilityStatusNotReachable)
        {
            UIAlertView * aler = [[UIAlertView alloc]initWithTitle:@"提示" message:@"当前没有网络,请您检查网络！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [aler show];
        
        }
        
        
    }];
}

// 下拉刷新数据
- (void)refreshData
{
    
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status == 0) {
            UIAlertView * aler = [[UIAlertView alloc]initWithTitle:@"提示" message:@"当前没有网络,请您检查网络！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [aler show];
            [self.Collec reloadData];
            [self.Collec.header endRefreshing];
        }else
        {
            [[DPSingleton defaultManager] reloadData];
            [[DPSingleton defaultManager] getDPdata:_i];
            [self.Collec reloadData];
            [self.Collec.header endRefreshing];
            if (self.counts == [DPSingleton defaultManager].DPdata.count) {
                self.Collec.header.hidden = YES;
            }

        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupFooter
{
    SDRefreshFooterView *refreshFooter = [SDRefreshFooterView refreshView];
    [refreshFooter addToScrollView:self.Collec];
    [refreshFooter addTarget:self refreshAction:@selector(footerRefresh)];
    _refreshFooter = refreshFooter;
}

// 上拉加载
- (void)footerRefresh
{
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status == 0) {
            UIAlertView * aler = [[UIAlertView alloc]initWithTitle:@"提示" message:@"当前没有网络,请您检查网络！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [aler show];
            [self.Collec reloadData];
            [self.refreshFooter endRefreshing];
        }else
        {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [[DPSingleton defaultManager] getDPdata:_x];
                _x = _x + 20;
//                _totalRowCount = [DPSingleton defaultManager].DPdata.count;
                [self.Collec reloadData];
                [self.refreshFooter endRefreshing];
            });
            
        }
    }];
    
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(self.view.bounds.size.width / 5 * 2.2, self.view.bounds.size.width / 5 * 3);
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

    
    return [DPSingleton defaultManager].DPdata.count;
    
    
}

- (DPCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    DPCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier: @"cell_id" forIndexPath:indexPath ];
    DPModel *model = [DPSingleton defaultManager].DPdata[indexPath.row];
    [cell setCellWithDPModel:model];

    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DPModel *mode = [[DPModel alloc]init];
    mode = [DPSingleton defaultManager].DPdata[indexPath.row];
    DPXQViewController * dvc = [[DPXQViewController alloc]init];
    dvc.idi = mode.ID;
    dvc.titelString = mode.name;
    dvc.descriptionString = mode.Description;
    dvc.urlImage = mode.cover_image_url;
    // push之前将tab bar隐藏。
    [dvc setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:dvc animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
